package com.scoresystem.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.IScoreDao;
import com.scoresystem.model.Score;
import com.scoresystem.model.StuScore;
import com.scoresystem.model.Student;
import com.scoresystem.utils.ResultMapUtil;

public class ScoreDaoImpl extends BaseDaoImpl implements IScoreDao {

	public ScoreDaoImpl(Connection conn) {
		super(conn);
		this.conn=conn;
	}

	@Override
	public List<StuScore> listAll(String stuclass, String couresename, String workname) {
		String sql="select stunum,stuname,score from score where stuclass="
				+"'"+stuclass+"'"+" and coursename="+"'"+couresename+"'"+
				" and worknum="+"'"+workname+"'";
		List<StuScore> list=new ArrayList<StuScore>();
		try{
			ResultSet rs=templateQuery(sql);
			while(rs.next()){
				Map<String,String> map=ResultMapUtil.getResultMap(rs);				
				StuScore stuscore=new StuScore();
				try {
					BeanUtils.populate(stuscore, map);
				} catch (Exception e) {
					e.printStackTrace();
				} 
				list.add(stuscore);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean insertScores(String stuclass, String coursename, String worknum, String[] stunum, 
			String[] score,String[] stuname) {
		try{
			for(int i=0;i<stunum.length;i++){
				String sql="insert into "+CommonValue.TAB_SCORE+" (stunum,worknum"
						+ ",coursename,score,stuclass,stuname)"+"values"+"("+
						"'"+stunum[i]+"'"+","+"'"+worknum+"'"+
						","+"'"+coursename+"'"+","
						+Integer.parseInt(score[i])+","+"'"+stuclass+"'"+","
						+"'"+stuname[i]+"'"+")";
				this.templateUpdate(sql);
			}
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return false;
			
	}

	@Override
	public String getScorebyClassCourse(String stunum,String stuclass, String coursename) {
		String sql="select score from score where stuclass="+"'"+stuclass+"'"
				+" and coursename="+"'"+coursename+"'"+" and stunum="+"'"+stunum+"'";
		System.out.println(sql);
		String result=null;
		try{
			ResultSet rs=templateQuery(sql);
			if(rs.next()){
				result=rs.getString(1);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<Score> listscores(String stunum) {
		String sql="select coursename,score,worknum from score where stunum="
				+"'"+stunum+"'";
		List<Score> list=new ArrayList<Score>();
		try{
			ResultSet rs=templateQuery(sql);
			while(rs.next()){
				Map<String,String> map=ResultMapUtil.getResultMap(rs);				
				Score score=new Score();
				try {
					BeanUtils.populate(score, map);
				} catch (Exception e) {
					e.printStackTrace();
				} 
				list.add(score);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public boolean modify(String stuclass, String coursename, String worknum, String stunum, String score) {
		String sql="update "+CommonValue.TAB_SCORE+" set score="+"'"+score+"'"
				+" where stunum="+"'"+stunum+"'"+" and worknum="+"'"+worknum+"'"
				+" and coursename="+"'"+coursename+"'"+" and stuclass="+"'"+stuclass+"'";
		try{
			this.templateUpdate(sql);
			return true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean insertScore(String stuclass, String coursename, String worknum, String stunum, String score,
			String stuname) {
		try{
		String sql="insert into "+CommonValue.TAB_SCORE+" (stunum,worknum"
				+ ",coursename,score,stuclass,stuname)"+"values"+"("+
				"'"+stunum+"'"+","+"'"+worknum+"'"+
				","+"'"+coursename+"'"+","
				+Integer.parseInt(score)+","+"'"+stuclass+"'"+","
				+"'"+stuname+"'"+")";
		this.templateUpdate(sql);
		return true;
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

}
